package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import lombok.val;
import org.example.domain.po.*;
import org.example.domain.vo.ErpDepotItemVO;
import org.example.service.*;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depot")
public class ErpDepotController {

    @Autowired
    ErpDepotHeadService erpDepotHeadService;
    @Autowired
    ErpSupplierService erpSupplierService;
    @Autowired
    ErpDepotService erpDepotService;
    @Autowired
    ErpUserService erpUserService;
    @Autowired
    ErpDepotItemService erpDepotItemService;

    //分页条件查询
    @GetMapping("/find")
    public R<Page<ErpDepotHead>> find(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "3") Integer size,
            String number,
            String status,
            String type,
            String subType,
            Long depId,
            Long organId,
            Long userId,
            String createTime,
            String endTime,
            String model
    ){
        Page<ErpDepotHead> page = new Page<>(current, size);
        page = erpDepotHeadService
                .findPage(page,number,status,type,subType,depId,organId,userId,createTime,endTime,model);
        return R.success(page);
    }
    @GetMapping("/look/{id}")
    public R<List<ErpDepotItemVO>> look(@PathVariable("id") Integer id){
        val look = erpDepotItemService.look(id);
        return R.success(look);
    }
//仓库入库新增
    @PostMapping("/insert")
    public Result insertDepot(@RequestBody DepotRequest depotRequest){
        ErpDepotHead erpDepotHead = depotRequest.getErpDepotHead();
        List<ErpDepotItem> erpDepotItems=depotRequest.getErpDepotItems();
        erpDepotHeadService.save(erpDepotHead);

        for(ErpDepotItem erpDepotItem:erpDepotItems){
            erpDepotItem.setHeaderId(erpDepotHead.getId());
            erpDepotItemService.save(erpDepotItem);
        }
        return Result.success();
    }

    @PutMapping("/update")//删除
    public Result update(@RequestBody UpdateStatusRequest updateStatusRequest){
        return Result.success(erpDepotHeadService.updateDelete(updateStatusRequest.getIds(), updateStatusRequest.getStatus()));
    }

    @PutMapping("/update/sh")//审核
    public Result updateStatus(@RequestBody UpdateStatusRequest updateStatusRequest){
        boolean b = erpDepotHeadService.updateStatus(updateStatusRequest.getIds(), updateStatusRequest.getStatus());
        if (b){
            return Result.success(b ? "成功" : "失败");
        } else {
            return Result.error("审核失败");
        }
    }

    @DeleteMapping("/remove/{id}")
    public Result<String> removeById(@PathVariable("id") Integer id){
        erpDepotHeadService.removeById(id);
        return Result.success("删除成功");
    }
    //查询供应商
    @GetMapping("/findSupplier/{supType}")
    public R<List<ErpSupplier>> findSupplier (@PathVariable("supType") Integer supType){
        return R.success(erpSupplierService.findSupplier(supType));
    }
    //查询仓库
    @GetMapping("/findDepot")
    public R<List<ErpDepot>> findDepot(@RequestAttribute("user") ErpUser user){
        LambdaQueryWrapper<ErpDepot> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ErpDepot::getTenantId, user.getTenantId());
        List<ErpDepot> list = erpDepotService.list(wrapper);
        return R.success(list);
    }

    
    //查询用户
    @GetMapping("/findUser")
    public R<List<ErpUser>> findUser(@RequestAttribute("user") ErpUser user){
        LambdaQueryWrapper<ErpUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ErpUser::getTenantId, user.getTenantId());
        List<ErpUser> list=erpUserService.list(wrapper);
        return R.success(list);
    }

    @GetMapping("/list")
    public R<List<ErpDepot>> selectByExample(){
        List<ErpDepot> list=erpDepotService.selectByExample();
        return R.success(list);
    }
    
    @GetMapping("/findDepotByTenantId")
    public Result<List<ErpDepot>> findDepotByTenantId(@RequestAttribute("user") ErpUser user){
        return Result.success(erpDepotService.findDepotByTenantId(user.getTenantId()));
    }




}
